package com.xbongbong.paas.enums;

import com.xbongbong.i18n.util.I18nMessageUtil;

import java.util.Objects;

/**
 * 计算字段汇总类型 （跟AggTypeEnum进行区分）
 *
 * @author zyy
 * @date 2022/07/26/ 18:46:00
 * @since v1.0
 */
public enum ComputedFieldAggTypeEnum {
    SUM(1, "sum", "enum.computedFieldAggType.name.sum"),
    AVG(2, "avg", "enum.computedFieldAggType.name.avg"),
    MIN(3, "min", "enum.computedFieldAggType.name.min"),
    MAX(4, "max", "enum.computedFieldAggType.name.max"),
    AGG(6,"agg","enum.computedFieldAggType.name.agg")
    ;

    private int code;
    private String alias;
    private String name;

    ComputedFieldAggTypeEnum(int code, String alias, String name) {
        this.code = code;
        this.alias = alias;
        this.name = name;
    }

    public int getCode() {
        return this.code;
    }

    public String getAlias() {
        return this.alias;
    }

    public String getName() {
        return I18nMessageUtil.getMessage(this.name);
    }

    /**
     * 根据code获取对应的枚举值，默认给SUM
     *
     * @param code
     * @return com.xbongbong.paas.enums.ComputedFieldAggTypeEnum
     * @author zyy
     * @date 2022/7/26
     * @version v1.0
     */
    public static ComputedFieldAggTypeEnum getByCode(Integer code) {
        if(code != null){
            for (ComputedFieldAggTypeEnum cacheCode : values()) {
                if (Objects.equals(cacheCode.getCode(), code)) {
                    return cacheCode;
                }
            }
        }
        //默认给sum
        return SUM;
    }


    /**
     * 根据alias获取对应的枚举值，默认给SUM
     *
     * @param alias
     * @return com.xbongbong.paas.enums.ComputedFieldAggTypeEnum
     * @author zyy
     * @date 2022/7/26
     * @version v1.0
     */
    public static ComputedFieldAggTypeEnum getByAlias(String alias) {
        if(alias != null){
            for (ComputedFieldAggTypeEnum cacheCode : values()) {
                if (Objects.equals(cacheCode.getAlias(), alias)) {
                    return cacheCode;
                }
            }
        }
        //默认给sum
        return SUM;
    }

    /**
     * 统计表使用 - 根据alias获取对应的name
     *
     * @param alias
     * @author zyy
     * @date 2022/7/26
     * @version v1.0
     */
    public static String getNameByAlias(String alias) {
        ComputedFieldAggTypeEnum computedFieldAggTypeEnum = SUM;
        if(alias != null){
            for (ComputedFieldAggTypeEnum cacheCode : values()) {
                if (Objects.equals(cacheCode.getAlias(), alias)) {
                    computedFieldAggTypeEnum = cacheCode;
                }
            }
        }
        return "( " + computedFieldAggTypeEnum.getName() + " )";
    }
}
